<template>
  <div>
    <el-button type="primary" @click="clickAddProduct">新增商品</el-button>
    <el-table :data="data.goodsTableData" style="width: 100%">
      <el-table-column prop="goodsId" label="商品溯源码" width="180"> </el-table-column>
      <el-table-column prop="name" label="商品名" width="180"> </el-table-column>
      <el-table-column prop="productId" label="原产品ID" width="180"> </el-table-column>
      <el-table-column prop="certification" label="进货证明" width="180">
        <template #default="scope">
          <el-button type="primary" @click="checkCertification(scope.row)">查看</el-button>
        </template>
      </el-table-column>

      <el-table-column prop="phone" label="联系方式" width="180"> </el-table-column>
      <el-table-column prop="exp" label="保质期" width="180"> </el-table-column>
    </el-table>

    <el-dialog
        v-model="data.addGoodsVisible"
        title="新增商品"
    >
      <el-form :model="data.addGoodsForm">
        <el-form-item label="商品溯源ID" :label-width="data.formLabelWidth">
          <el-input v-model="data.addGoodsForm.goodsId" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品名" :label-width="data.formLabelWidth">
          <el-input v-model="data.addGoodsForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="原产品ID" :label-width="data.formLabelWidth">
          <el-input v-model="data.addGoodsForm.productId" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" :label-width="data.formLabelWidth">
          <el-input v-model="data.addGoodsForm.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="保质期" :label-width="data.formLabelWidth">
          <el-input v-model="data.addGoodsForm.exp" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="进货证明" :label-width="data.formLabelWidth">
          <UploadPic @getUploadHash="getHash"/>
        </el-form-item>
      </el-form>
      <template #footer>
          <span class="dialog-footer">
            <el-button @click="data.addGoodsVisible = false">取消</el-button>
            <el-button type="primary" @click="handleAddGoods">确认</el-button>
          </span>
      </template>
    </el-dialog>

    <el-dialog v-model="data.certificationVisible">
      <img class="certification" :src="data.certificationSrc" />
    </el-dialog>
  </div>
</template>

<script>
import {onMounted, reactive} from "vue";
import {v1 as uuidv1} from "uuid";
import {
  func_addGoods,
  func_getAddress, func_getProductById, func_getUserGoods,
  func_getUserInfo,
  func_getUserProduct,
  func_getUserRole
} from "../../../api/etherneum/api";
import {useStore} from "vuex";
import {useRouter} from "vue-router";
import UploadPic from "@/components/UploadPic/index";
import ipfsAPI from "ipfs-api";
import {ElMessage} from "element-plus";
const ipfs = ipfsAPI({ host: "49.234.138.61", port: "5001", protocol: "http" });
export default {
  name: "Sales",
  components:{UploadPic},
  setup(){
    const data=reactive({
      addGoodsVisible:false,
      addGoodsForm:{},
      goodsTableData:[],
      formLabelWidth:'140px',
      certificationVisible:false,
      certificationSrc:''

    })
    const store=useStore();
    const router=useRouter();
    const clickAddProduct=()=>{
      if (data.curUserInfo.role!=1&&data.curUserInfo.role!=5){
        ElMessage.error('无权限')
        return
      }
      data.addGoodsVisible=true
      data.addGoodsForm.goodsId=uuidv1()
    }
    const handleAddGoods=async ()=>{
      if (!data.addGoodsForm.certification){
        ElMessage.error('图片未上传')
        return
      }
      let productInfo=await func_getProductById(data.addGoodsForm.productId)
      if (productInfo.status==0){
        ElMessage.error('产品未完成加工')
        return
      }
      await func_addGoods(data.addGoodsForm)
      data.addGoodsVisible=false
    }
    const getAddress = async () => {
      const address = await func_getAddress();
      if (address) {
        store.commit("app/SET_ADDRESS", address);
        let role = await func_getUserRole(address);
        store.commit("app/SET_ROLE", role);
        console.log("getUser address=" + address + " role=" + role);
        data.address = address;
        data.currentRole = role;
        data.curUserInfo = await func_getUserInfo(data.address);
        console.log("cur:" + data.curUserInfo.requestRole);
      }
    };
    const getHash=(hash)=>{
      data.addGoodsForm.certification=hash
    }

    const checkCertification=async (row)=>{
      let hash=row.certification
      data.loading=true
      console.log('certification:',row.certification)
      ipfs.get(hash, async (err, result) => {
        console.log('here')
        if (err) throw err;
        let binaryData = [];
        binaryData.push(result[0].content.buffer);
        data.certificationSrc= window.URL.createObjectURL(new Blob(binaryData))
        console.log('src=',data.certificationSrc)
        data.loading=false
      });

      data.certificationVisible=true
    }
    onMounted(()=>{
      getAddress().then(async () => {
        data.goodsTableData=await func_getUserGoods(data.address)
        console.log(data.goodsTableData)

      });
      ethereum.on("accountsChanged", () => {
        getAddress();
      });
    })
    return{
      data,
      clickAddProduct,
      handleAddGoods,
      checkCertification,
      getHash
    }
  }
}
</script>

<style lang="scss" scoped>
.certification{
  width: 800px;
}
</style>